# coding=utf-8

from scene import Scene
import pygame
import pygame_gui

import group1
import group2
import group3
from question import test

import ques_grp_chs_01 # 中文试题
import ques_grp_chs_02 # 中文试题

import ques_grp_eng_01 # 英文试题

WIN_WIDTH = 1600
WIN_HEIGHT = 800

LABEL_HEIGHT = WIN_HEIGHT // 14
ANSWER_HEIGHT = LABEL_HEIGHT
QUESTION_HEIGHT = 2 * LABEL_HEIGHT

#ques_lib = group1.gen_ques(50, swap=True)
#ques_lib = group2.gen_ques(50, category=20)
ques_lib = group3.gen_ques(50, swap=True)

class SceneConsoleTestChoose(Scene):

    def __init__(self):
        self.is_gui = False

        # 语文
        grps_chs = []
        ques_grp_chs_01.register(grps_chs)
        ques_grp_chs_02.register(grps_chs)

        # 数学
        grps_math = [
            group2.test(),
            group2.test_multi(),
            group2.test_div(),
        ]

        # 英语
        grps_eng = []
        ques_grp_eng_01.register(grps_eng)

        self.categories = [
            ("语文", grps_chs),
            ("数学", grps_math),
            ("英语", grps_eng),
        ]

        self.ques_nums = [
            ("共 5 题", 5),
            ("共 10 题", 10),
            ("共 20 题", 20),
            ("共 50 题", 50),
            ("共 100 题", 100),
        ]
        

    @staticmethod
    def menu_select(caption, prompt, items, default_sel, show_func = None):
        while True:
            print("---", caption, "---")
            for i, grp in enumerate(items):
                if show_func:
                    # print(grp)
                    show_func(i, grp, default_sel)
                else:
                    if default_sel == i:
                        print("[<%d>] : %s" % (i, grp))
                    else:
                        print("[ %d ] : %s" % (i, grp))

            sel = input((">>> %s " % prompt.replace("##", "[%d]")) % default_sel)
            try:
                if sel == "":
                    sel = default_sel
                else:
                    sel = int(sel)
                if sel >= 0 and sel < len(items):
                    return sel
            except:
                if sel.strip() == "q":
                    import sys
                    sys.exit(-1)

            print("!!! 请输入大于等 0 且小于 %d 的整数 !!!\n" % len(items))


    def menu(self, config):
        params = {}

        category_id = config.get("CategoryId", 0)
        ctgrs = [x[0] for x in self.categories]
        sel = SceneConsoleTestChoose.menu_select("范围", "请选择范围序号 ##：", ctgrs, category_id)
        config.set("CategoryId", sel)
        print(sel)

        grps = self.categories[sel][1]
        group_id = config.get("GroupId", 0)
        sel = SceneConsoleTestChoose.menu_select("试题组", "请选择试题组序号 ##：", grps, group_id, lambda i, grp, default_sel: print("[%s] : %s （题数：%d）" % (("<%d>" if i == default_sel else " %d ") % i , grp.summary, grp.GetQuestionCount())))
        config.set("GroupId", sel)
        print(sel)
        params["question_group"] = grps[sel]

        ques_num_desc = [x[0] for x in self.ques_nums]
        count_id = config.get("CountId", 0)
        sel = SceneConsoleTestChoose.menu_select("试题数目", "请选择试题数目序号 ##：", ques_num_desc, count_id)
        print(sel)
        config.set("CountId", sel)
        ques_num = self.ques_nums[sel][1]
        params["question_count"] = ques_num

        config.save()
        return params


    def activate(self, params = None):
        print("params = ", params)


    def process_events(self, events):
        # x = input("x = ")
        # print(x)
        # y = input("y = ")
        # print(y)

        params = {}
        return self.switch("test", params)


if __name__ == "__main__":
    scene = SceneConsoleTestChoose()
    params = scene.menu()
    print(params)
